package com.band.controller;


import com.band.entity.Dept;
import com.band.entity.Result;
import com.band.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     *
     * 查询所有部门
     * @return
     */
    @GetMapping
    public Result list(){
        List<Dept> dept = deptService.list();
        return Result.success(dept);
    }

    /**
     *
     * 删除部门
     * @param id 部门id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id){
        log.info("删除部门：{}", id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     *
     * 新增部门
     * @param dept 部门对象
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody Dept dept){
        log.info("新增部门：{}", dept);
        deptService.insert(dept);
        return Result.success();
    }

    /**
     *
     * 查询部门  数据回显
     * @param id 部门id
     * @return
     */
    @GetMapping("/{id}")
    public Result select(@PathVariable Integer id){
        log.info("查询部门：{}", id);
        Dept dept = deptService.select(id);
        return Result.success(dept);
    }

    /**
     *
     * 更新部门
     * @param dept 部门对象
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("更新部门：{}", dept);
        deptService.update(dept);
        return Result.success();
    }

}
